package com.itzixi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzixi.common.pojo.JqGridResult;
import com.itzixi.mapper.TestMapper;
import com.itzixi.pojo.Test;
import com.itzixi.service.TestService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Service
public class TestServiceImpl implements TestService{


    @Autowired
    private TestMapper testMapper;

    @Autowired
    private Sid sid;

    @Override
    public void saveTest(Test test) {
        String itemId = sid.nextShort();
        test.setId(itemId);
        testMapper.insert(test);
    }

    @Override
    public JqGridResult queryTestList(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Test> list =  testMapper.selectAll();
        PageInfo<Test> pageList = new PageInfo<Test>(list);

        JqGridResult grid = new JqGridResult();
        grid.setTotal(pageList.getPages());
        grid.setRows(list);
        grid.setPage(pageList.getPageNum());
        grid.setRecords(pageList.getTotal());
        return grid;
    }

    @Override
    public Test queryTestById(String testId) {
        return testMapper.selectByPrimaryKey(testId);
    }

    @Override
    public void deleteTestById(String testId) {
        testMapper.deleteByPrimaryKey(testId);
    }

    @Override
    public void updateTestById(Test test) {
        testMapper.updateByPrimaryKey(test);
    }
}
